package com.liu.activemq.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

public class JmsConsumer_Topic {
    public static final String ACTIVEMQ_URL = "tcp://192.168.25.133:61616";
    public static final String TOPIC_NAME = "topic01";

    public static void main(String[] args) throws JMSException, IOException {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);

        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(TOPIC_NAME);

        MessageConsumer consumer = session.createConsumer(topic);


//        while (true){
//            TextMessage receive = (TextMessage)consumer.receive();
//            if (receive!=null){
//                System.out.println(receive.getText());
//            }else {
//                break;
//            }
//        }
        session.setMessageListener(new MessageListener() {

            public void onMessage(Message message) {
                TextMessage receive = (TextMessage) message;
                try {
                    System.out.println(receive.getText());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        System.in.read();
        consumer.close();
        session.close();
        connection.close();

    }
}
